<?php
include("../connect/connect.php");
$id = $_GET['id'];

if($_POST['fname'] !="" && $_POST['lname'] !="" && $_POST['name'] !="" && $_POST['phone'] !="" && $_POST['fax'] !="" && $_POST['email'] !=""){
	$date = date("Y-m-d H:i:s");
	//mysql_query("INSERT INTO reservation_detail (reservation_detail_fname,reservation_detail_lname,reservation_detail_name,reservation_detail_phone,reservation_detail_fax,reservation_detail_email,reservation_detail_detail,reservation_detail_payment_information,group_detail_id,discount,discount2,reservation_detail_sale_name,reservation_detail_sale_phone,reservation_detail_time) VALUES
	//																																										 ('".$_POST['fname']."','".$_POST['lname']."','".$_POST['name']."','".$_POST['phone']."','".$_POST['fax']."','".$_POST['email']."','".$_POST['remark']."','".$_POST['tResep']."','".$_POST['Group_Detail']."','".$_POST['discount']."','".$_POST['discount2']."','".$_POST['name_r']."','".$_POST['phone_r']."','$date')");
	mysql_query("UPDATE reservation_detail SET reservation_detail_fname='".$_POST['fname']."',reservation_detail_lname='".$_POST['lname']."',reservation_detail_name='".$_POST['name']."',reservation_detail_phone='".$_POST['phone']."',reservation_detail_fax='".$_POST['fax']."',reservation_detail_email='".$_POST['email']."',reservation_detail_detail='".$_POST['remark']."',reservation_detail_payment_information='".$_POST['tResep']."',group_detail_id='".$_POST['Group_Detail']."',discount='".$_POST['discount']."',discount2='".$_POST['discount2']."',reservation_detail_sale_name='".$_POST['name_r']."',reservation_detail_sale_phone='".$_POST['phone_r']."',reservation_detail_time_edit='$date' WHERE reservation_detail_id='$id'");
}

//เลือกข้อมูลมาบันทึกลงข้อมูลการจอง reservation
//$iQuery = mysql_query("SELECT max(reservation_detail_id) as reservation_detail_id FROM reservation_detail");
//$iRow = mysql_fetch_array($iQuery);
//$reservation_detail_id = $iRow['reservation_detail_id'];
mysql_query("DELETE FROM reservation WHERE reservation_detail_id='$id'");
$aQuery = mysql_query("SELECT * FROM reservation_session WHERE reservation_detail_id='$id'");
while($aRow = mysql_fetch_array($aQuery)){
	mysql_query("INSERT INTO reservation (room_id,reservation_checkin,reservation_detail_id,reservation_status) VALUES ('".$aRow['room_id']."','".$aRow['reservation_checkin']."','$id','".$aRow['reservation_status']."')");
}

//กรณีเลือก Group
if($_POST['tReseps']=='group'){
	mysql_query("DELETE FROM reservation_information WHERE reservation_detail_id='$id'");
	$gCount = count($_POST['list']);
	$cCount = 0;
	for($i=0;$i<$gCount;$i++){
		if($_POST['list'][$i]!="" && $_POST['amount'][$i]!="" && $_POST['price'][$i]!=""){
			$cCount = $cCount+1;
			mysql_query("INSERT INTO reservation_group (reservation_detail_id,reservation_group_list,reservation_group_amount,reservation_group_price) VALUES ('$id','".$_POST['list'][$i]."','".$_POST['amount'][$i]."','".$_POST['price'][$i]."')");
		}
	}
} else {
	mysql_query("DELETE FROM reservation_group WHERE reservation_detail_id='$id'");
	mysql_query("DELETE FROM reservation_information WHERE reservation_detail_id='$id'");
	
	if($_POST['Abf']!=""){
		$AbfQuery = mysql_query("SELECT more_information_price FROM more_information WHERE more_information_id = '1'");
		$AbfRow = mysql_fetch_array($AbfQuery);
		mysql_query("INSERT INTO reservation_information (reservation_detail_id,more_information_id,more_information_value,more_information_price) VALUE ('".$id."','1','".$_POST['tAbf']."','".$AbfRow['more_information_price']."')");
	}

	if($_POST['ExtraAbf']!=""){
		$tExtraAbfQuery = mysql_query("SELECT more_information_price FROM more_information WHERE more_information_id = '2'");
		$tExtraAbfRow = mysql_fetch_array($tExtraAbfQuery);
		mysql_query("INSERT INTO reservation_information (reservation_detail_id,more_information_id,more_information_value,more_information_price) VALUE ('".$id."','2','".$_POST['tExtraAbf']."','".$tExtraAbfRow['more_information_price']."')");
	}
	if($_POST['Extra']!=""){
		$tExtraQuery = mysql_query("SELECT more_information_price FROM more_information WHERE more_information_id = '3'");
		$tExtraRow = mysql_fetch_array($tExtraQuery);
		mysql_query("INSERT INTO reservation_information (reservation_detail_id,more_information_id,more_information_value,more_information_price) VALUE ('".$id."','3','".$_POST['tExtra']."','".$tExtraRow['more_information_price']."')");
	}
	if($_POST['AbfA']!=""){
		$tAbfAQuery = mysql_query("SELECT more_information_price FROM more_information WHERE more_information_id = '4'");
		$tAbfARow = mysql_fetch_array($tAbfAQuery);
		mysql_query("INSERT INTO reservation_information (reservation_detail_id,more_information_id,more_information_value,more_information_price) VALUE ('".$id."','4','".$_POST['tAbfA']."','".$tAbfARow['more_information_price']."')");
	}
	if($_POST['AbfC']!=""){
		$tAbfCQuery = mysql_query("SELECT more_information_price FROM more_information WHERE more_information_id = '5'");
		$tAbfCRow = mysql_fetch_array($tAbfCQuery);
		mysql_query("INSERT INTO reservation_information (reservation_detail_id,more_information_id,more_information_value,more_information_price) VALUE ('".$id."','5','".$_POST['tAbfC']."','".$tAbfCRow['more_information_price']."')");
	}
	if($_POST['AddA']!=""){
		$tAddAQuery = mysql_query("SELECT more_information_price FROM more_information WHERE more_information_id = '6'");
		$tAddARow = mysql_fetch_array($tAddAQuery);
		mysql_query("INSERT INTO reservation_information (reservation_detail_id,more_information_id,more_information_value,more_information_price) VALUE ('".$id."','6','".$_POST['tAddA']."','".$tAddARow['more_information_price']."')");
	}
	if($_POST['AddC']!=""){
		$tAddCQuery = mysql_query("SELECT more_information_price FROM more_information WHERE more_information_id = '7'");
		$tAddCRow = mysql_fetch_array($tAddCQuery);
		mysql_query("INSERT INTO reservation_information (reservation_detail_id,more_information_id,more_information_value,more_information_price) VALUE ('".$id."','7','".$_POST['tAddC']."','".$tAddCRow['more_information_price']."')");
	}

}

echo "<script>window.location='room_edit_print.php?id=".$id."';</script>";
?>
